const app = getApp();
const cancelText = "取消",
  deleteText = "删除",
  confirmText = "确定";
const {
  validate
} = require("../../../../utils/validate")
Page({
  data: {
    keyAOptions: [], //秘钥A
    keyBOptions: [], //秘钥B
    storage_location_options: [
      {name: "不校验区域码", value: "0"},
      {name: "位置一", value: "1"},
      {name: "位置二", value: "2"},
      {name: "位置三", value: "3"}
    ], //存储位置
    keyAIndex: 0, //A秘钥索引  
    keyBIndex: 0, //B秘钥索引
    storage_location_index:0,//存储位置索引
    regionName: "", //名称
    regionCode: "", //区域码
    show: false,
    itemList: [
      "A", "B", "C", "D", "E", "F", 1, 2, 3, 4, 5, 6, 7, 8, 9, cancelText, 0, deleteText
    ],
    numbers: [{
      value: 0,
      checked: false,
    }, {
      value: 0,
      checked: false,
    }, {
      value: "",
      checked: true,
    }, {
      value: "",
      checked: false,
    }, {
      value: "",
      checked: false,
    }, {
      value: "",
      checked: false,
    }]
  },


  onLoad: function (options) {
    this.from = options.from || '';
    this.getKeyOptions();
  },

  //1通用区域码 2兼容
  getKeyOptions() {
    app.ajax({
      url: "Common/Common/getCardMotherKeyConfig",
      data: {
        type: 1
      },
      success: res => {
        if (res.data.code == 1000) {
          const options = res.data.data;
          this.setData({
            keyAOptions: options.key,
            keyBOptions: options.key_b,
          })
        }
      }
    })
  },
  changeKey(e) {
    const name = e.currentTarget.dataset.name;
    const index = e.detail.value;
    this.setData({
      [name]: index
    })
  },


  confirmBtn() {
    const name = this.data.regionName;
    if (validate._isNullOrEmpty(name)) {
      app.showToast("请输入区域码名称");
      return;
    }
    const area_code = this.data.regionCode;
    if (this.data.keyAIndex === '') {
      app.showToast("请选择A秘钥");
      return;
    }
    let key = this.data.keyAOptions[this.data.keyAIndex].value;
    let key_b;
    if (this.data.keyBIndex !== '') {
      key_b = this.data.keyBOptions[this.data.keyBIndex].value;
    }

    if (this.data.storage_location_index === '') {
      app.showToast("请选择存储位置");
      return;
    }
    let storage_location = this.data.storage_location_options[this.data.storage_location_index].value;

    let is_frozen = 1; //默认为正常
    wx.showLoading({
      mask: true,
    })
    app.ajax({
      url: "Partner/CardMother/addCardMother",
      data: {
        name,
        key,
        key_b,
        area_code,
        is_frozen,
        storage_location
      },
      success: res => {
        wx.hideLoading();
        if (res.data.code == 1000) {
          app.showToast(res.data.message, "success");
          setTimeout(() => {
            const pages = getCurrentPages();
            if (this.from == 'middle') {
              let targetPage = pages[pages.length - 3];
              targetPage.setModal(res.data.data);
              wx.navigateBack({
                delta: 2,
              })
            } else {
              let targetPage = pages[pages.length - 2];
              targetPage.onLoad(targetPage.options);
              wx.navigateBack()
            }
          }, 1500)
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading({})
      }
    })
  },




  handleClose() {
    this.setData({
      show: false
    })
  },

  showNumberInput(e) {
    wx.hideKeyboard();
    this.setData({
      show: true
    })
  },

  chooseNumber(e) {
    const value = e.currentTarget.dataset.value;
    if (value == cancelText) { //取消
      this.setData({
        show: false
      })
    } else if (value == deleteText) { //删除
      this.deleteNumber();
    } else if (value == confirmText) {
      this.confirmNumber();
    } else {
      this.numberInput(value);
    }
  },


  confirmNumber() {
    let regionCode = [];
    this.data.numbers.forEach(current => regionCode.push(current.value));
    this.setData({
      regionCode: regionCode.join(""),
      show: false
    })
  },

  deleteNumber() {
    const numbers = this.data.numbers;
    let index = numbers.findIndex(current => current.checked == true);
    numbers[index].value = "";
    if (index == 0) {
      numbers[index].checked = true;
    } else {
      numbers[index].checked = false;
      numbers[index - 1].checked = true;
    }
    this.setData({
      numbers
    })
    if (index == numbers.length - 1 || index == 0) {
      let idx = this.data.itemList.findIndex(current => current == (index == 0 ? cancelText : confirmText));
      let txt = index == 0 ? confirmText : cancelText;
      if (~idx) {
        this.setData({
          [`itemList[${idx}]`]: txt
        })
      }
    }
  },

  numberInput(value) {
    const numbers = this.data.numbers;
    let index = numbers.findIndex(current => current.checked == true);
    numbers[index].value = value;
    if (index + 1 == numbers.length) {
      numbers[index].checked = true;
    } else {
      numbers[index].checked = false;
      numbers[index + 1].checked = true;
    }
    this.setData({
      numbers
    })
    if (index + 1 == numbers.length || index == 0) {
      let idx = this.data.itemList.findIndex(current => current == (index == 0 ? confirmText : cancelText));
      let txt = index == 0 ? cancelText : confirmText;
      if (~idx) {
        this.setData({
          [`itemList[${idx}]`]: txt
        })
      }
    }
  },
})